package com.qst.yunpan.service;

import com.qst.yunpan.dao.ShareDao;
import com.qst.yunpan.pojo.Share;
import com.qst.yunpan.pojo.ShareFile;
import com.qst.yunpan.pojo.User;
import com.qst.yunpan.utils.FileUtils;
import com.qst.yunpan.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ShareService {
    @Autowired
    private ShareDao shareDao;
    /**
     * 分享文件
     * @param request
     * @param currentPath
     * @param shareFile
     * @return
     * @throws Exception
     */
    public String shareFile(HttpServletRequest request, String currentPath, String[] shareFile) throws Exception {
        String username = (String) request.getSession().getAttribute(User.NAMESPACE);
        //获取分享链接代码
        String shareUrl = FileUtils.getUrl8();
        for (String file : shareFile) {
            //遍历要分享的文件的名称
            Share share = new Share();
            share.setPath(currentPath + File.separator + file);
            share.setShareUser(username);
            share.setShareUrl(shareUrl);
            //调用了shareDao.shareFile方法往数据库中添加一次分享记录
            shareDao.shareFile(share);
        }
        return shareUrl;
    }
    public List<ShareFile> findShare(HttpServletRequest request, String shareUrl) throws Exception{
        Share share = new Share();
        share.setShareUrl(shareUrl);
        share.setStatus(Share.PUBLIC);
        //返回列表，一次分享链接可能对应多个文件
        List<Share> shares = shareDao.findShare(share);
        return getShareFile(request, shares);
    }
    private List<ShareFile> getShareFile(HttpServletRequest request, List<Share> shares){
        List<ShareFile> files = null;
        if(shares != null){
            files = new ArrayList<ShareFile>();
            String rootPath = request.getSession().getServletContext().getRealPath("/") + FileService.PREFIX;
            for (Share share : shares) {
                //拿到本次链接对应的所有文件的真实路径，把相应的文件信息封装进shareFile中
                File file = new File(rootPath + share.getShareUser(), share.getPath());
                ShareFile shareFile = new ShareFile();
                shareFile.setFileType(FileUtils.getFileType(file));
                shareFile.setFileName(file.getName());
                shareFile.setFileSize(file.isFile() ? FileUtils.getDataSize(file.length()) : "-");
                shareFile.setLastTime(FileUtils.formatTime(file.lastModified()));
                shareFile.setShareUser(share.getShareUser());
                shareFile.setUrl(share.getShareUrl());
                shareFile.setFilePath(share.getPath());
                files.add(shareFile);
            }
        }
        return files;
    }
    public List<ShareFile> findShareByName(HttpServletRequest request, int status) throws Exception{
        List<Share> shares = shareDao.findShareByName(UserUtils.getUsername(request), status);
        return getShareFile(request, shares);
    }
    public String cancelShare(String url, String filePath, int status) throws Exception {
        //从失效链接页面过来，即目前的分享已经是失效的分享
        if(Share.CANCEL == status){
            shareDao.cancelShare(url, filePath, Share.DELETE);
            return "删除成功";
        }else{
            shareDao.cancelShare(url, filePath, Share.CANCEL);
            return "链接已失效";
        }
        //不会真正在数据库中删除，只会改变状态
    }
}
